package com.yanzhu.api;

import com.yanzhu.bean.ResponseBean;
import com.yanzhu.service.RechargeService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author: 方炎柱
 * @Description: 充值模块
 * @Date: Created in 22:15 2018/1/21
 * @Modified:
 */
@AllArgsConstructor
@RestController
public class RechargeAPI {
    private RechargeService rechargeService;

//    //充值
//    @RequestMapping(value="/recharge", method= RequestMethod.POST)
//    public ResponseBean insertRecharge(HttpServletRequest request){
//        String uid = request.getParameter("uid");
//        String recharge_fee = request.getParameter("recharge_fee");
//
//        if(uid == null || uid.equals(""))
//            return ResponseBean.fail("缺少用户ID参数！");
//
//        if(recharge_fee == null || recharge_fee.equals(""))
//            return ResponseBean.fail("缺少充值金额参数！");
//
//        Recharge recharge = new Recharge();
//        recharge.setUid(Integer.parseInt(uid));
//        recharge.setRecharge_fee(Float.parseFloat(recharge_fee));
//        recharge.setRecharge_time((int)(new Date().getTime()/1000));
//        rechargeService.insert(recharge);
//        return ResponseBean.ok("充值成功！");
//    }

    @RequestMapping(value="/queryUserRechargeList")
    public ResponseBean queryRechargeList(HttpServletRequest request){
        String uid = request.getParameter("uid");
        if(uid == null || uid.equals(""))
            return ResponseBean.fail("缺少用户ID参数！");

        String page = request.getParameter("page");
        if(page == null || page.equals(""))
            return ResponseBean.fail("缺少页数参数!");

        String size = request.getParameter("size");
        if(size == null || size.equals(""))
            return ResponseBean.fail("缺少页数大小参数!");
        return ResponseBean.ok(rechargeService.queryRechargeList(Long.parseLong(uid), Integer.parseInt(page), Integer.parseInt(size)));
    }

    @RequestMapping(value="/deleteUserRechargeById")
    public ResponseBean deleteById(HttpServletRequest request){
        String id = request.getParameter("id");

        if(id == null || id.equals(""))
            return ResponseBean.fail("缺少ID参数！");

        if(rechargeService.deleteRechargeById(Long.parseLong(id)) < 0)
            return ResponseBean.fail("数据库错误！");

        return ResponseBean.ok();
    }

    @RequestMapping(value="/deleteAllRecharge")
    public ResponseBean deleteAll(HttpServletRequest request){
        String uid = request.getParameter("uid");

        if(uid == null || uid.equals(""))
            return ResponseBean.fail("缺少用户ID参数！");
        if(rechargeService.deleteRechargeAll(Long.parseLong(uid)) < 0)
            return ResponseBean.fail("数据库错误！");

        return ResponseBean.ok();
    }

}
